<?php
/**
 * Description
 * User: duanwenjie
 * Date: 2019/5/9
 * Time: 11:59 AM
 */

namespace app\index\service;
use app\common\model\ToolModel;
use think\Db;
use think\Exception;

class ShopService
{
    static public function getShopList($param, $is_download = false)
    {
        try{
            $where = "where 1 = 1 ";
            $pageNumber = isset($param['page']) ? $param['page'] : 1;
            $pageData = isset($param['limit']) ? $param['limit'] : 20;
            $shop_name = isset($param['shop_name']) ? $param['shop_name'] : '';  //店铺名称
            $shop_id = isset($param['id']) ? $param['id'] : '';  //店铺代码
            $country_name = isset($param['country_name']) ? $param['country_name'] : '';  //所属国家
            $status = isset($param['status']) ? $param['status'] : '';  //状态
            $type = isset($param['type']) ? $param['type'] : '';  //店铺类型
            $offset = ($pageNumber - 1) * $pageData;
            $limit = " LIMIT $offset,$pageData";

            if (!empty($shop_name)) {
                $shop_name = trim($shop_name);
                $where .= " AND BS.shop_name = '{$shop_name}'";
            }
            if (!empty($shop_id)) {
                $shop_id = trim($shop_id);
                $where .= " AND BS.id = {$shop_id}";
            }
            if (!empty($country_name)) {
                $country_name = trim($country_name);
                $where .= " AND BS.base_country_name = '{$country_name}'";
            }
            if (!empty($status)) {
                $where .= " AND BS.status = {$status}";
            }
            if (!empty($type)) {
                $where .= " AND BS.type = {$type}";
            }

            $sqlCount = "SELECT COUNT(BS.id) num FROM base_shop BS {$where}";
            $countArr = ToolModel::queryData($sqlCount);
            if (empty($countArr) || $countArr[0]['num'] == 0) {
                return array('total' => 0, 'list' => array());
            }
            $fileds = 'BS.id,BS.shop_name,BS.base_country_name,BS.address,
                      case BS.type when 1 then "自营" when 2 then "联营" when 3 then "加盟" end type_name,
                      case BS.status when 1 then "启用" when -1 then "禁用" end status_name,BS.type,BS.status,
                      BS.sap,BS.responsibility_name,BS.responsibility_phone,BC.company_name,ER.name as currency_name,BS.operator,BS.update_time'; //

            $sql = "SELECT {$fileds} FROM base_shop BS LEFT JOIN base_company BC ON BS.base_company_id = BC.id
            LEFT JOIN exchange_rate ER ON BS.exchange_rate_id = ER.id {$where} ORDER BY BS.id DESC {$limit}";
            if ($is_download) $sql = "SELECT {$fileds} FROM base_shop BS LEFT JOIN base_company BC ON BS.base_company_id = BC.id {$where} ORDER BY BS.id DESC ";

            $list = ToolModel::queryData($sql);
            $data = array();
            $data['total'] = intval($countArr[0]['num']);
            $data['list'] = $list;

            return $data;
        }catch (Exception $e){
            trace($e->getMessage(), 'error');
            throw new Exception($e->getMessage());
        }
    }


    /**
     * 校验新增/编辑数据
     * @param $params
     * @param string $id
     * @return array
     * @throws Exception
     * @author duanwenjie
     */
    static public function checkData($params, $id = "")
    {
        try{
            $company_name = $params['company_name'];
            $shop_name = $params['shop_name']; // exchange_rate
            $currency_name = $params['currency_name'];
            $sap = $params['sap'];
            /*校验公司*/
            $res = Db::name('base_company')->where('company_name', '=', $company_name)->find();
            /*校验结算货币*/
            $res2 = Db::name('exchange_rate')->where('name', '=', $currency_name)->find();
            if (empty($res)) returnJson(200, "操作失败！公司：({$company_name})不存在");
            if (empty($res2)) returnJson(200, "操作失败！结算货币：({$currency_name})不存在");
            $base_company_id = $res['id']; // 公司ID
            $base_country_id = $res['base_country_id']; // 国家ID
            $base_country_name = $res['base_country_name']; // 国家名称
            $exchange_rate_id = $res2['id']; // 结算货币ID
            if (empty($id)){
                /*新增需校验店铺*/
                $res3 = Db::name('base_shop')->where('shop_name', '=', $shop_name)->find();
                if (!empty($res3)) returnJson(200, "操作失败！店铺：({$shop_name})已存在");
                $res6 = Db::name('base_shop')->where('sap', '=', $sap)->find();
                if (!empty($res6)) returnJson(200, "操作失败！SAPCODE码：({$sap})已存在");
            }else{
                $res_ = Db::name('base_shop')->where('id', '=', $id)->find();
                if (isset($res_['shop_name']) && $res_['shop_name'] != $params['shop_name']){
                    /*编辑的店铺名称发生改变时需校验*/
                    $res4 = Db::name('base_shop')->where('shop_name', '=', $shop_name)->find();
                    if (!empty($res4)) returnJson(200, "操作失败！店铺：({$shop_name})已存在");
                }
                if (isset($res_['sap']) && $res_['sap'] != $params['sap']){
                    /*编辑的sap发生改变时需校验*/
                    $res5 = Db::name('base_shop')->where('sap', '=', $sap)->find();
                    if (!empty($res5)) returnJson(200, "操作失败！SAPCODE码：({$sap})已存在");
                }
            }
            $data = array("base_company_id" => $base_company_id, "base_country_id" => $base_country_id, "base_country_name" => $base_country_name, "exchange_rate_id" => $exchange_rate_id);

            return $data;
        }catch (Exception $e){
            trace($e->getMessage(), 'error');
            throw new Exception($e->getMessage());
        }
    }

    /**
     * 店铺更新联动更新仓库相关数据
     * @param $base_shop_id
     * @param $base_country_id
     * @param $base_country_name
     * @param $base_company_id
     * @author duanwenjie
     */
    static public function updateWarehouse($base_shop_id, $base_country_id, $base_country_name, $base_company_id)
    {
        $res = ToolModel::updateData(array("base_shop_id" => $base_shop_id),array("base_country_id" => $base_country_id,"base_country_name" => $base_country_name,"base_company_id" => $base_company_id),'base_warehouse');

    }


}